package com.ncr.travel.appmgr.client.presenter.handler;

import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;

import com.google.web.bindery.autobean.shared.AutoBean;
import com.google.web.bindery.autobean.shared.AutoBeanFactory;
import com.google.web.bindery.autobean.shared.AutoBeanVisitor;
import com.ncr.travel.appmgr.client.AppController;
import com.ncr.travel.appmgr.client.gin.Injector;
import com.ncr.travel.appmgr.client.model.ApplicationConfigurationFactory;
import com.ncr.travel.appmgr.client.model.ScreenConfigurationFactory;
import com.ncr.travel.appmgr.client.mvp.EventBus;
import com.ncr.travel.appmgr.client.service.AppManagerWebService;
import com.ncr.travel.appmgr.model.ScreenConfigurationJson;
import com.ncr.travel.appmgr.model.shared.AuditField;
import com.ncr.travel.appmgr.model.shared.AvailableGraphics;
import com.ncr.travel.appmgr.model.shared.ColumnSpecification;
import com.ncr.travel.appmgr.model.shared.ErrorText;
import com.ncr.travel.appmgr.model.shared.FontOptions;
import com.ncr.travel.appmgr.model.shared.Function;
import com.ncr.travel.appmgr.model.shared.RowSpecification;
import com.ncr.travel.appmgr.model.shared.Rule;
import com.ncr.travel.appmgr.model.shared.ScreenConfiguration;
import com.ncr.travel.appmgr.model.shared.TextEntries;
import com.ncr.travel.appmgr.model.shared.StationsAuditCriteria;
import com.ncr.travel.appmgr.model.shared.TableDefinition;
import com.ncr.travel.appmgr.model.shared.Text;
import com.ncr.travel.appmgr.model.shared.TextEntry;
import com.ncr.travel.appmgr.model.shared.ErrorTexts;
import com.octo.gwt.test.GwtCreateHandler;
import com.octo.gwt.test.utils.GwtReflectionUtils;

public class InjectorHandler implements GwtCreateHandler {

	@Override
	public Object create(Class<?> classLiteral) throws Exception {
		if (classLiteral.isAssignableFrom(Injector.class)) {
			return new Injector() {

				@Override
				public EventBus getEventBus() {
					// TODO Auto-generated method stub
					return null;
				}

				@Override
				public AppManagerWebService getWebService() {
					// TODO Auto-generated method stub
					return null;
				}

				@Override
				public AppController getAppController() {
					// TODO Auto-generated method stub
					return null;
				}

				@Override
				public ApplicationConfigurationFactory getApplicationConfigurationFactory() {
					// TODO Auto-generated method stub
					return null;
				}

				@Override
				public ScreenConfigurationFactory getScreenConfigurationFactory() {
					return new ScreenConfigurationFactory() {

						@Override
						public <T> AutoBean<T> create(Class<T> clazz) {
							return null;
						}

						@Override
						public <T, U extends T> AutoBean<T> create(Class<T> clazz, U delegate) {
							return null;
						}

						@Override
						public AutoBean<ScreenConfiguration> screenConfiguration() {
							return new AutoBean<ScreenConfiguration>() {

								@Override
								public void accept(AutoBeanVisitor visitor) {
									// TODO Auto-generated method stub
									
								}

								@Override
								public ScreenConfiguration as() {
									ScreenConfiguration screenConfiguration = new ScreenConfigurationJson();
									return screenConfiguration;
								}

								@Override
								public AutoBean<ScreenConfiguration> clone(boolean deep) {
									// TODO Auto-generated method stub
									return null;
								}

								@Override
								public AutoBeanFactory getFactory() {
									// TODO Auto-generated method stub
									return null;
								}

								@Override
								public <Q> Q getTag(String tagName) {
									// TODO Auto-generated method stub
									return null;
								}

								@Override
								public Class<ScreenConfiguration> getType() {
									// TODO Auto-generated method stub
									return null;
								}

								@Override
								public boolean isFrozen() {
									// TODO Auto-generated method stub
									return false;
								}

								@Override
								public boolean isWrapper() {
									// TODO Auto-generated method stub
									return false;
								}

								@Override
								public void setFrozen(boolean frozen) {
									// TODO Auto-generated method stub
									
								}

								@Override
								public void setTag(String tagName, Object value) {
									// TODO Auto-generated method stub
									
								}

								@Override
								public ScreenConfiguration unwrap() {
									// TODO Auto-generated method stub
									return null;
								}
								
							};
						}

						@Override
						public AutoBean<AuditField> auditField() {
							// TODO Auto-generated method stub
							return null;
						}

						@Override
						public AutoBean<AvailableGraphics> availableGraphics() {
							// TODO Auto-generated method stub
							return null;
						}

						@Override
						public AutoBean<ColumnSpecification> columnSpecification() {
							// TODO Auto-generated method stub
							return null;
						}

						@Override
						public AutoBean<ErrorText> errorText() {
							// TODO Auto-generated method stub
							return null;
						}

						@Override
						public AutoBean<FontOptions> fontOptions() {
							// TODO Auto-generated method stub
							return null;
						}

						@Override
						public AutoBean<RowSpecification> rowSpecification() {
							// TODO Auto-generated method stub
							return null;
						}

						@Override
						public AutoBean<Rule> rule() {
							// TODO Auto-generated method stub
							return null;
						}

						@Override
						public AutoBean<StationsAuditCriteria> stationsAuditCriteria() {
							// TODO Auto-generated method stub
							return null;
						}

						@Override
						public AutoBean<TableDefinition> tableDefinition() {
							// TODO Auto-generated method stub
							return null;
						}

						@Override
						public AutoBean<TextEntry> textEntry() {
							// TODO Auto-generated method stub
							return null;
						}

						@Override
						public AutoBean<Function> function() {
							// TODO Auto-generated method stub
							return null;
						}

                        @Override
                        public AutoBean<Text> text(Text source) {
                            // TODO Auto-generated method stub
                            return null;
                        }

                        @Override
                        public AutoBean<TextEntries> TextEntries() {
                            // TODO Auto-generated method stub
                            return null;
                        }

                        @Override
                        public AutoBean<ErrorTexts> ErrorTexts() {
                            // TODO Auto-generated method stub
                            return null;
                        }
						
					};
				}
				
			};
		}
		return doDefault(classLiteral);
	}

	@SuppressWarnings("unchecked")
	public Object doDefault(Class<?> classLiteral) throws Exception {
		if (classLiteral.isAnnotation() || classLiteral.isArray() || classLiteral.isEnum() || classLiteral.isInterface() || Modifier.isAbstract(classLiteral.getModifiers())) {
			return null;
		}

		try {
			Constructor<Object> cons = (Constructor<Object>) classLiteral.getDeclaredConstructor();
			GwtReflectionUtils.makeAccessible(cons);
			return GwtReflectionUtils.instantiateClass(cons);
		} catch (NoSuchMethodException e) {
			return null;
		}
	}
}
